﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using System.Web;
using System.Web.Caching;
using LotteryVote.Data;
using LotteryVote.Logic.Exceptions;

namespace LotteryVote.Logic.Bet
{
    internal abstract class MultipleCalculator
    {
        private const string TODAYRESULT = "TodayLotteryResult";
        private LotteryResultFacade _lotteryResultFacade;
        public LotteryResultFacade LotteryResultFacade
        {
            get
            {
                if (_lotteryResultFacade == null)
                    _lotteryResultFacade = new LotteryResultFacade();
                return _lotteryResultFacade;
            }
            set
            {
                _lotteryResultFacade = value;
            }
        }

        private NumLengthDataAccess _daNumLength;
        internal NumLengthDataAccess DaNumLength
        {
            get
            {
                if (_daNumLength == null)
                    _daNumLength = new NumLengthDataAccess();
                return _daNumLength;
            }
            set
            {
                _daNumLength = value;
            }
        }

        private LotteryCompanyDataAccess _daCompany;
        internal LotteryCompanyDataAccess DaCompany
        {
            get
            {
                if (_daCompany == null)
                    _daCompany = new LotteryCompanyDataAccess();
                return _daCompany;
            }
            set
            {
                _daCompany = value;
            }
        }

        private CompanyTypeDataAccess _daCompanyType;
        internal CompanyTypeDataAccess DaCompanyType
        {
            get
            {
                if (_daCompanyType == null)
                    _daCompanyType = new CompanyTypeDataAccess();
                return _daCompanyType;
            }
            set
            {
                _daCompanyType = value;
            }
        }

        /// <summary>
        /// 今日开奖结果.
        /// </summary>
        public IEnumerable<LotteryResult> TodayLotteryResult
        {
            get
            {
                var result = HttpContext.Current.Cache[TODAYRESULT] as IEnumerable<LotteryResult>;
                if (result == null)
                {
                    result = LotteryResultFacade.GetLotteryResultByDate(DateTime.Today);
                    //将今日开奖结果放到缓存中，并在5分钟没操作后清空缓存
                    HttpContext.Current.Cache.Add(TODAYRESULT, result, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5), CacheItemPriority.Default, null);
                }
                return result;
            }
        }
        protected abstract IDictionary<int, IEnumerable<string>> LotteryResult { get; }
        public abstract int Calculate(BetOrder order);
        protected abstract void ClearLotteryResult();
        protected IEnumerable<string> GetCompanyLotteryResult(int companyId)
        {
            IEnumerable<string> result = LotteryResult.ContainsKey(companyId) ? LotteryResult[companyId] : null;
            if (result == null && TodayLotteryResult != null)
            {
                HttpContext.Current.Cache.Remove(MultipleCalculator.TODAYRESULT);
                ClearLotteryResult();
                if (!LotteryResult.ContainsKey(companyId))
                    throw new PromptException("没有开奖号码!");
                result = LotteryResult[companyId];
            }
            return result;
        }
    }
}
